import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
     /*   long time = new Date().getTime();
        System.out.println(time);*/
        //insertMp3();
        insertString();
    }

    private static void insertString() throws UnsupportedEncodingException, FileNotFoundException, ClassNotFoundException, SQLException {
        File file = new File("F:\\OKOKOK.txt");
        Scanner scanner = new Scanner(new InputStreamReader(new FileInputStream(file),"UTF-8"));
        List<String> all = new ArrayList<>();
        while(scanner.hasNextLine()){
            String line = scanner.nextLine().trim();
            if(line.length()>0){
                if(line.split("@").length!=2){
                    System.out.println(line);
                }else{
                    all.add(line.trim());
                }
                //String[] split = line.substring((line.indexOf("@") + 1)).split("@");
              /*  String idTxt = line.substring(line.indexOf("@")+1,line.lastIndexOf("@")).trim();
                String content = line.substring((line.lastIndexOf("@")+1)).trim();
                all.add(idTxt+","+content);
                System.out.println(idTxt+","+content);*/
            }
        }

        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://106.14.143.105:3306/driver?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull","root","svNn#d2kD%");
        System.out.println(connection);
        connection.setAutoCommit(false);
        String sql  = " update subject set `explain` = ? where id = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i <all.size(); i++) {
            String[] idconent = all.get(i).split("@");
            Integer id = Integer.parseInt(idconent[0]);
            String content = idconent[1].trim();
            content = "<span style=\"font-size:18px;\"><strong>" + content+ "</strong></span>";
            preparedStatement.setString(1,content);
            preparedStatement.setInt(2,id);
            preparedStatement.addBatch();
            if((i+1) % 200==0){
                preparedStatement.executeBatch();
                preparedStatement.clearBatch();
            }
        }

         preparedStatement.executeBatch();
        connection.commit();
        System.out.println("OK!!!!!");

    }


    /**
     * 插入mp3 文件
     * @throws Exception
     */
    public static void insertMp3() throws Exception {
        File file = new File("D:\\斑马线数据\\语音备份\\技巧语音—覆盖");
        String[] ids = file.list();
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://106.14.143.105:3306/driver?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull","root","svNn#d2kD%");
        System.out.println(connection);
        connection.setAutoCommit(false);
        for (int i = 0; i < ids.length; i++) {
            if (ids[i].matches("\\d{1,}e\\.mp3")) {
                ids[i] = ids[i].trim().substring(0, ids[i].indexOf("."));
                System.out.println(ids[i]);
            }else {
                String s = ids[i];
                System.out.println(s);
               // new File(file,ids[i]).renameTo(new File(s.substring(0,s.indexOf("."))+"r.mp3"));
               /* System.out.println( s.substring(0,s.indexOf(".")));
                new File(s.substring(0,s.indexOf("."))+"r.mp3").renameTo(new File(file,ids[i]));
                System.out.println(i);*/
            }
        }
        //
     /*  String sql  = " update subject set mpath = ? ,mtype=0 where id = ? ";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

       for (int i = 1; i <= ids.length; i++) {
            Integer id = Integer.parseInt(ids[i-1].trim().substring(0,ids[i-1].indexOf("e")));
            preparedStatement.setString(1,"/upload/music/"+id+"e.mp3");
            preparedStatement.setInt(2,id);
            preparedStatement.addBatch();
            if(i % 200==0){
                preparedStatement.executeBatch();
                preparedStatement.clearBatch();
            }
        }
        preparedStatement.executeBatch();
        connection.commit();
        System.out.println("OK!!!!!");*/
    }
}
